package com.zhx.web;

import com.zhx.domain.LoginCommand;
import com.zhx.domain.User;
import com.zhx.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import sun.misc.Request;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;

/**
 * @program: LoginController
 * @author: zhx
 * @create: 2021-03-23 12:29
 **/
@Controller //标注成为一个springmvc的controller
@RequestMapping(value = "/user")
public class LoginController {

    private static Logger logger = LoggerFactory.getLogger(LoginController.class);

    private UserService userService;

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    //负责处理/index.html的请求
    @RequestMapping(value = "/index.html")
    public String loginPage() {
        logger.info("进入登录界面");
        return "login";
    }

    //进入注册页面
    @RequestMapping(value = "/toRegisterUser.html")
    public String toRegisterUser(){
        logger.info("进入注册页面");
        return "register";
    }

    //负责处理/loginCheck.html的请求
    @RequestMapping(value = "/loginCheck.html")
    public ModelAndView loginCheck(HttpServletRequest request, LoginCommand loginCommand) {
        try {
            //检查user缓冲中是否有这个用户
            boolean lock = userService.isLock(loginCommand.getUserName());
            if (!lock){ //user缓冲中有这个用户，需要锁定1分钟
                return new ModelAndView("login", "error", "需要锁定1分钟");
            }
            logger.info("开始检查用户名和密码");
            boolean isValidUser = userService.hasMatchUser(loginCommand.getUserName(), loginCommand.getPassWord());
            if (!isValidUser) {
                logger.info("用户名或密码错误");
                return new ModelAndView("login", "error", "用户名或密码错误");
            } else {
                User user = userService.findUserByUserName(loginCommand.getUserName());
                user.setLastIp(request.getLocalAddr());
                user.setLastvisit(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis()));
                userService.loginSuccess(user);
                request.getSession().setAttribute("user", user);
                logger.info("用户名或密码正确，返回到主页面");
                return new ModelAndView("loginSuccess");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.info("登陆失败，检查错误！");
            return new ModelAndView("login", "error", "登陆失败，检查错误！");
        }
    }


    //负责处理注册的请求
    @RequestMapping(value = "/registerUse.html")
    public ModelAndView registerUser(HttpServletRequest request , LoginCommand loginCommand){
        try {
            logger.info("开始注册");
            boolean b = userService.registerUser(loginCommand);
            if (b){
                logger.info("注册成功");
                return new ModelAndView("login");
            }else{
                logger.info("注册失败");
                request.setAttribute("error","注册失败，请重新注册" );
                return new ModelAndView("register");
            }
        }catch (Exception e){
            e.printStackTrace();
            request.setAttribute("error","注册失败，请重新注册" );
            return new ModelAndView("register");
        }
    }
}
